import axios from 'axios'
import type { WarehouseData } from './signalr'

// 创建axios实例
const api = axios.create({
  baseURL: 'http://localhost:5212/api',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    console.log('发送请求:', config.url)
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    console.log('收到响应:', response.data)
    return response
  },
  (error) => {
    console.error('请求错误:', error)
    return Promise.reject(error)
  }
)

// Storage数据接口定义
export interface StorageData {
  storageId: number
  storageCode: string
  storageName: string
  storageCapacity: number
  storageAdder: string
  department_Id: number
  department_Name: string
  user_WorkCode: string
  user_Name: string
  locationNumder: number
  storagestate: boolean
  loginUserName: string
  operationTime?: string
}

export interface StorageApiResponse {
  totalCount: number
  totalPCount: number
  data: StorageData[]
  code: number
  msg: string
}

// Storage API接口定义
export const storageAPI = {
  // 获取仓库列表
  async getStorageList(params: {
    PageIndex: number
    PageSize: number
    storageCode?: string
    storageName?: string
    storageState?: boolean | null
  }): Promise<StorageApiResponse> {
    try {
      const response = await api.get('/Storage/GetStorage', { params })
      return response.data
    } catch (error) {
      console.error('获取仓库列表失败:', error)
      throw error
    }
  },

  // 新增仓库
  async addStorage(data: Partial<StorageData>): Promise<any> {
    try {
      const response = await api.post('/Storage/AddStorage', data)
      return response.data
    } catch (error) {
      console.error('新增仓库失败:', error)
      throw error
    }
  },

  // 更新仓库
  async updateStorage(data: Partial<StorageData>): Promise<any> {
    try {
      const response = await api.put('/Storage/UpdateStorage', data)
      return response.data
    } catch (error) {
      console.error('更新仓库失败:', error)
      throw error
    }
  },

  // 删除仓库
  async deleteStorage(storageId: number): Promise<any> {
    try {
      const response = await api.delete(`/Storage/DeleteStorage/${storageId}`)
      return response.data
    } catch (error) {
      console.error('删除仓库失败:', error)
      throw error
    }
  },

  // 启用仓库
  async enableStorage(storageId: number): Promise<any> {
    try {
      const response = await api.put(`/Storage/EnableStorage/${storageId}`)
      return response.data
    } catch (error) {
      console.error('启用仓库失败:', error)
      throw error
    }
  },

  // 停用仓库
  async disableStorage(storageId: number): Promise<any> {
    try {
      const response = await api.put(`/Storage/DisableStorage/${storageId}`)
      return response.data
    } catch (error) {
      console.error('停用仓库失败:', error)
      throw error
    }
  },

  // 锁定仓库
  async lockStorage(storageId: number): Promise<any> {
    try {
      const response = await api.put(`/Storage/LockStorage/${storageId}`)
      return response.data
    } catch (error) {
      console.error('锁定仓库失败:', error)
      throw error
    }
  }
}

// API接口定义
export const storageStockAPI = {
  // 获取库存数据
  async getStorageStock(): Promise<WarehouseData[]> {
    try {
      const response = await api.get('/ChatHub/GetStorageStock')
      if (response.data.code === 200) {
        return response.data.data.map((item: any) => ({
          storageId: item.storageId,
          name: `仓库${item.storageId}`,
          quantity: item.quantity
        }))
      } else {
        throw new Error(response.data.message || '获取库存数据失败')
      }
    } catch (error) {
      console.error('获取库存数据失败:', error)
      throw error
    }
  },

  // 更新库存数据
  async updateStorageStock(data: Partial<WarehouseData>): Promise<void> {
    try {
      const response = await api.post('/ChatHub/UpdateStorageStock', data)
      if (response.data.code !== 200) {
        throw new Error(response.data.message || '更新库存数据失败')
      }
    } catch (error) {
      console.error('更新库存数据失败:', error)
      throw error
    }
  }
}

export default api 